package com.android.view.activity.trade;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.model.entity.res.OrderCompletedResult;
import com.android.model.info.LocalSp;
import com.android.presenter.iview.IOrderCompletedView;
import com.android.util.DateUtils;
import com.android.util.log.LogUtil;
import com.android.view.activity.base.ToolbarBaseActivity;
import com.android.R;
import com.android.presenter.info.CompletedOrderPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 所有历史委托页面
 * Created by TenFace on 2018/5/3.
 */

public class OrderHistoryActivity extends ToolbarBaseActivity implements IOrderCompletedView {

    @BindView(R.id.rvAllOpenOrders)
    RecyclerView rvAllOpenOrders;
    private CompletedOrderPresenter completedOrderPresenter;
    private String accountId;
    private String orderTradeToken;

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_order_history);
    }

    @Override
    protected void initView() {
        completedOrderPresenter = new CompletedOrderPresenter(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvAllOpenOrders.setLayoutManager(linearLayoutManager);
    }

    @Override
    protected void obtainData() {
        notifyOpenOrders();
    }

    @Override
    protected void initEvent() {

    }


    private void notifyOpenOrders() {
        accountId = LocalSp.getInstance(getContext()).getAccountInfoId();
        orderTradeToken = LocalSp.getInstance(getContext()).getOrderTradeToken();
        LogUtil.e(accountId);
        LogUtil.e(orderTradeToken);
        if (!TextUtils.isEmpty(accountId) && !TextUtils.isEmpty(orderTradeToken)) {
            completedOrderPresenter.getListCompletedOrder(accountId, 1, 100, orderTradeToken);
        }
    }


    @Override
    public void orderCompletedSuccess(OrderCompletedResult orderResultList) {
        if (orderResultList != null) {
            LogUtil.e("orderOpenSuccess");
            OrderHistoryAdapter orderHistoryAdapter = new OrderHistoryAdapter(getContext(), orderResultList);
            rvAllOpenOrders.setAdapter(orderHistoryAdapter);
            orderHistoryAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void orderCompletedFail(int status, String desc) {

    }

    public class OrderHistoryAdapter extends RecyclerView.Adapter<OrderHistoryAdapter.Ex55Holder> {

        private Context mContext;
        private List<OrderCompletedResult.DataBean> list;

        public OrderHistoryAdapter(Context context,OrderCompletedResult orderResultList) {
            super();
            this.mContext = context;
            this.list = orderResultList.getData();
        }

        @Override
        public Ex55Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_open_orders, null);
            Ex55Holder ex55Holder = new Ex55Holder(view);
            return ex55Holder;
        }

        @Override
        public void onBindViewHolder(Ex55Holder holder, final int position) {
            holder.tvSymbol.setText(list.get(position).getSymbol());
            holder.tvStatus.setText(list.get(position).getOrderStatus());
            holder.tvPrice.setText(String.valueOf(list.get(position).getLimitPrice()));
            holder.tvAmount.setText(String.valueOf(list.get(position).getOpenQuantity()));
            holder.tvOrderDateTime.setText(DateUtils.getDateToStringYMD(list.get(position).getCreatedAt()));
            holder.tvOrderTime.setText(DateUtils.getDateToStringHms(list.get(position).getCreatedAt()));
            holder.tvCancel.setText(list.get(position).getOrderStatus());
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Ex55Holder extends RecyclerView.ViewHolder {

            @BindView(R.id.tvSymbol)
            TextView tvSymbol;
            @BindView(R.id.tvStatus)
            TextView tvStatus;
            @BindView(R.id.tvPrice)
            TextView tvPrice;
            @BindView(R.id.tvAmount)
            TextView tvAmount;
            @BindView(R.id.tvOrderDateTime)
            TextView tvOrderDateTime;
            @BindView(R.id.tvOrderTime)
            TextView tvOrderTime;
            @BindView(R.id.tvCancel)
            TextView tvCancel;

            public Ex55Holder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }


}
